//
//  SXNewFeatureController.m
//  Weibo1
//
//  Created by dfpo on 15/3/16.
//  Copyright (c) 2015年 dfpo. All rights reserved.
//

#import "SXNewFeatureController.h"
#import "SXTabBarController.h"
#define SXNewFeatureImageCount 4
@interface SXNewFeatureController ()<UIScrollViewDelegate>
@property (nonatomic, weak) UIPageControl *pageControl;
@property (nonatomic, weak) UIScrollView * scrollView;
@end

@implementation SXNewFeatureController

- (void)viewDidLoad {
    [super viewDidLoad];
    // 实例化scrollView
    UIScrollView *scrollView = [[UIScrollView alloc] init];
    scrollView.frame = self.view.bounds;
    [self.view addSubview:scrollView];
    self.scrollView = scrollView;
    // 加载图片
    CGFloat scrollW = scrollView.width;
    CGFloat scrollH = scrollView.height;
    for (int i = 0; i< SXNewFeatureImageCount; i++) {
        UIImageView *imageView = [[UIImageView alloc] init];
        imageView.width = scrollW;
        imageView.height = scrollH;
        imageView.y = 0;
        imageView.x = i * scrollW;
        NSString *name = [NSString stringWithFormat:@"new_feature_%d", i + 1];
        imageView.image = [UIImage imageNamed:name];
        [scrollView addSubview:imageView];
        // 在最后一张图片上加载其他内容
        if (i == SXNewFeatureImageCount - 1) {
            [self setupLastImageView: imageView];
        }
    }
    // 设置让scrollView正常滚动
    scrollView.contentSize = CGSizeMake(SXNewFeatureImageCount * scrollW, 0);
    scrollView.bounces = NO; // 不要弹簧效果
    scrollView.pagingEnabled = YES;
    scrollView.showsHorizontalScrollIndicator = NO;
    scrollView.delegate = self;
    // 添加分页
    UIPageControl *pageControl = [[UIPageControl alloc] init];
    pageControl.numberOfPages = SXNewFeatureImageCount;
    pageControl.backgroundColor = [UIColor redColor];
    pageControl.currentPageIndicatorTintColor = SXColor(253, 98, 42);
    pageControl.pageIndicatorTintColor = SXColor(189, 189, 189);
    pageControl.centerX = scrollW * 0.5;
    pageControl.centerY = scrollH - 50;
    [self.view addSubview:pageControl];
    self.pageControl = pageControl;
}
- (void)setupLastImageView:(UIImageView *)imageView {
    // 让内部控件可交互
    imageView.userInteractionEnabled = YES;
    // 分享给大家（checkbox）
    UIButton *shareBtn = [[UIButton alloc] init];
    [shareBtn setImage:[UIImage imageNamed:@"new_feature_share_false"] forState:UIControlStateNormal];
    [shareBtn setImage:[UIImage imageNamed:@"new_feature_share_true"] forState:UIControlStateSelected];
    [shareBtn setTitle:@"分享给大家" forState:UIControlStateNormal];
    [shareBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    shareBtn.titleLabel.font = [UIFont systemFontOfSize:15];
    shareBtn.width = 200;
    shareBtn.height = 30;
    shareBtn.centerX = imageView.width * 0.5;
    shareBtn.centerY = imageView.height * 0.65;
    [shareBtn addTarget:self action:@selector(shareBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    [imageView addSubview:shareBtn];
    shareBtn.titleEdgeInsets = UIEdgeInsetsMake(0, 10, 0, 0);
    // 开始微博按钮
    UIButton *startBtn = [[UIButton alloc] init];
    [startBtn setBackgroundImage:[UIImage imageNamed:@"new_feature_finish_button"] forState:UIControlStateNormal];
    [startBtn setBackgroundImage:[UIImage imageNamed:@"new_feature_finish_button_highlighted"] forState:UIControlStateHighlighted];
    startBtn.size = startBtn.currentBackgroundImage.size;
    startBtn.centerX = shareBtn.centerX;
    startBtn.centerY = imageView.height * 0.75;
    [startBtn setTitle:@"开始微博" forState:UIControlStateNormal];
    [startBtn addTarget:self action:@selector(startBtnClick) forControlEvents:UIControlEventTouchUpInside];
    [imageView addSubview:startBtn];
    
}

- (void)shareBtnClick:(UIButton *)button {
    // 打勾打来打去的
    button.selected = ! button.isSelected;
}
- (void)startBtnClick {
    // 切换到SXTabBarController
    UIWindow *window = [UIApplication sharedApplication].keyWindow;
    window.rootViewController = [[SXTabBarController alloc] init];
}

- (void)dealloc
{
    SXLog(@"SXNewfeatureController-dealloc");
}
#pragma mark - UIScrollViewDelegate
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    double page = scrollView.contentOffset.x / scrollView.width;
    self.pageControl.currentPage = (int)(page + 0.5);
}


@end
